<template>
  <div>
    <el-form :rules="rules" :model="account" ref="fromRef">
      <el-form-item label="账号" prop="name">
        <el-input v-model="account.name" clearable />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="account.password"
          type="password"
          clearable
          show-password
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { defineComponent, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { rules } from './config/account-config'
import LocalCache from '@/utils/cache'

export default defineComponent({
  setup() {
    const store = useStore()
    const account = reactive({
      name: LocalCache.getCache('name') ?? '',
      password: LocalCache.getCache('password') ?? ''
    })
    //获取from的dom
    const fromRef = ref()
    const loginaction = (isKeeppassword) => {
      //通过from的dom可以获取表单预验证的方法
      fromRef.value?.validate((valid) => {
        if (valid) {
          //1.判断是否需要记住密码
          if (isKeeppassword.value) {
            LocalCache.setCache('name', account.name)
            LocalCache.setCache('password', account.password)
          } else {
            LocalCache.deletCache('name')
            LocalCache.deletCache('password')
          }
          //开始进行登录验证
          store.dispatch('login/accountLoginAction', { ...account })
        } else {
          console.log('验证失败')
        }
      })
    }

    return {
      account,
      rules,
      loginaction,
      fromRef
    }
  }
})
</script>

<style lang="less" scoped></style>
